/**
 * Copyright &copy; 2018-2018 <a href="#">聚成科技</a> All rights reserved.
 */
package com.jckj.core.service.impl;

import com.jckj.core.annotation.CurrentDataSource;
import com.jckj.core.dao.OfficeMapper;
import com.jckj.core.dao.OfficeUserMapper;
import com.jckj.core.dao.SysUserMapper;
import com.jckj.core.db.DataSourceEnum;
import com.jckj.core.entity.*;
import com.jckj.core.exception.ServiceException;
import com.jckj.core.service.OfficeService;
import com.jckj.core.vos.LoginUserVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 科室服务实现类
 * </p>
 *
 * @author 刘志远
 * @date 18-1-25
 */
@Service
@Transactional(readOnly = true, rollbackFor = ServiceException.class)
public class OfficeServiceImpl implements OfficeService {

    private static Logger LOGGER = LoggerFactory.getLogger(OfficeServiceImpl.class);

    @Autowired
    OfficeMapper officeMapper;

    @Autowired
    SysUserMapper sysUserMapper;

    @Autowired
    OfficeUserMapper officeUserMapper;

    @Autowired
    LoginUserVo loginUserVo;

    @Override
    @Transactional(readOnly = false, rollbackFor = ServiceException.class)
    public void add(Office office) throws ServiceException {
        try {
            officeMapper.insert(office);
        } catch (Exception ex) {
            LOGGER.error("保存科室信息：%s", ex);
            throw new ServiceException(ex);
        }
    }

    @Override
    @Transactional(readOnly = false, rollbackFor = ServiceException.class)
    public int update(Office office) throws ServiceException {
        int effectCount = 0;
        try {
            effectCount = officeMapper.updateByPrimaryKeySelective(office);
        } catch (Exception ex) {
            LOGGER.error("修改科室信息：%s", ex);
            throw new ServiceException(ex);
        }
        return effectCount;
    }

    @Override
    @Transactional(readOnly = false, rollbackFor = ServiceException.class)
    public int delete(String officeId) throws ServiceException {
        int effectCount = 0;
        try {
            effectCount = officeMapper.deleteByPrimaryKey(officeId);
        } catch (Exception ex) {
            LOGGER.error("删除科室信息：%s", ex);
            throw new ServiceException(ex);
        }
        return effectCount;
    }

    @Override
    @Transactional(readOnly = false, rollbackFor = ServiceException.class)
    public int addUser(SysUser user, String officeId) throws ServiceException {
        int effectCount = 0;
        try {
            sysUserMapper.insert(user);
            OfficeUserKey officeUser = new OfficeUserKey();
            officeUser.setOfficeId(officeId);
            officeUser.setUserId(user.getId());
            effectCount = officeUserMapper.insert(officeUser);
        } catch (Exception ex) {
            LOGGER.error("增加科室人员：%s", ex);
            throw new ServiceException(ex);
        }
        return effectCount;
    }

    @Override
    @CurrentDataSource(DataSourceEnum.SLAVE)
    public List<Office> listAll() {
        OfficeExample example = new OfficeExample();
        return officeMapper.selectByExample(example);
    }

    @Override
    @CurrentDataSource(DataSourceEnum.SLAVE)
    public long checkOfficeCode(String officeCode){
        OfficeExample example = new OfficeExample();
        OfficeExample.Criteria criteria = example.createCriteria();
        criteria.andOfficeCodeEqualTo(officeCode);
        return officeMapper.countByExample(example);
    }

    @Override
    @CurrentDataSource(DataSourceEnum.SLAVE)
    public List<Office> findOfficeByPid(String pid) {
        OfficeExample example = new OfficeExample();
        OfficeExample.Criteria criteria = example.createCriteria();
        if(pid==null){
            criteria.andPidIsNull();
        }else{
            criteria.andPidEqualTo(pid);
        }
        return officeMapper.selectByExample(example);
    }
}
